package model

import (
	"novel-mgr1/userrpc/userrpc"

	"gorm.io/gorm"
)

type Comment struct {
	gorm.Model
	Uid      int64  `gorm:"type:int;not null"`
	Nid      int64  `gorm:"type:int;not null"`
	Comments string `gorm:"type:varchar(30);not null"`
}

func (c *Comment) CommentAdd(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

func CommentList(db *gorm.DB, in *userrpc.CommentListRequest) []*userrpc.CommentList {
	var list []*userrpc.CommentList
	db.Debug().Model(&Comment{}).Select("comments.uid,users.nickname as nick_name,comments.comments as comment").Joins("left join users on users.id = comments.uid").Where("comments.nid=?", in.Nid).Order("comments.created_at desc").Find(&list)
	return list
}
